<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$config = array(
		//
		'cname' => 'item',
		
		//
		'class_file' => 'module/member/member.class.php',
		'class_name' => 'MemberBase',
		'class_param' => array('_table_status_usable' => false),
		
		//
		'inject' => array(
				'categories_options' => array(
									'type' => 'cm',
									'class_file' => 'module/member/member.category.class.php', 
									'class_name' => 'MemberCategoryBase',
									'method' => 'getCategoriesTreeFormat',
									'params' => array('', 0, false),
										),
					),
		
		//
		'tpl_type' => 'admin',
		'tpl_file' => 'pagelayout/pagelayout_admin_member.tpl',
		
		//
		'validate_file' => '',
		'validate_class' => 'CommonValidate',
		'validate_fields' => array('field' => 'name', 'valid' => 'null'),
			);

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.control.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.content.control.class.php');

//
class _ControlItemContentBase extends ControlItemContentBase {
	
	//constructor
	function _ControlItemContentBase($config = '') {
		parent::ControlItemContentBase($config);
		
		//
		if( $this->feObj->action == 'updatestatus' ) {
			$this->actionHandler = 'doUpdateStatusActionHandler';
		} elseif( $this->feObj->action == 'changepassword' ) {
			$this->actionHandler = 'doChangePasswordActionHandler';
		}
		//
		if( $this->feObj->operation == 'detail' && $this->feObj->_request['id'] > 0 ) {
			$this->displayHandler = 'doDetailDisplayHandler';
		} elseif( $this->feObj->operation == 'password' && $this->feObj->_request['id'] > 0 ) {
			$this->displayHandler = 'doChangePasswordDisplayHandler';
		}
	}
	
	//methods
	function doActionHandler() {
		echo 'doActionHandler';
		exit;
	}
	
	function doUpdateStatusActionHandler($obj) {
		
		_dms(':admin:success');
		$this->_redirect('', array('op'), 's');
	}
	
	function doChangePasswordActionHandler($obj) {
		global $userObj;
		global $rt_current_userid;
		
		$uid = $this->feObj->_post['id'];
		$old_password = util_get_param($_POST, 'field_old_password');
		$new_password = util_get_param($_POST, 'field_new_password');
		$confirm_password = util_get_param($_POST, 'field_confirm_password');
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES_VALIDATE . 'account.validate.class.php');
		$vObj = new AccountValidate();
		$vObj->checkPassword($_POST);
		if( $vObj->_valid() ) {
			$valid = $userObj->checkPassword($rt_current_userid, $old_password);
			if( $valid ) {
				$userObj->updatePassword($uid, $new_password);
				//
				_dms(':admin:success');
				$this->_redirect('', array('op', 'op' => 'detail'), 's');
			} else {
				$vObj->_add('field_old_password', 'your password is invalid');
			}
		}
		//
		$vm = $vObj->_result(':error');
		$this->feObj->addInject('vm', $vm);
		$vr = $vObj->_valid_result();
		$this->feObj->addInject('vr', $vr);
	}
	
	//
	function doDisplayHandler() {
		echo 'doDisplayHandler';
		exit;
	}
	
	function doDetailDisplayHandler($obj) {
		$item = $this->modelObj->getItem($this->feObj->_request['id'], 'id');
		if( $item['id'] == 0 ) {
			//
			_dms(':member:no_exist');
			//
			$mainpart = 'no_exist';
		} else {
			$this->feObj->content_wrap['item'] = $item;
			$mainpart = 'detail';
		}
		//
		$this->feObj->content_wrap['mainpart'] = $mainpart;
	}
	
	function doChangePasswordDisplayHandler($obj) {
		$item = $this->modelObj->getItem($this->feObj->_request['id'], 'id');
		if( $item['id'] == 0 ) {
			//
			_dms(':member:no_exist');
			//
			$mainpart = 'no_exist';
		} else {
			$this->feObj->content_wrap['item'] = $item;
			$mainpart = 'change_password';
		}
		//
		$this->feObj->content_wrap['mainpart'] = $mainpart;
	}
	
	//
	function _doAfter() {
		
	}
}

//
$cntlObj = new _ControlItemContentBase($config);
$cntlObj->process();
//
?>